package com.dt.common.validation.parser;

import java.lang.reflect.Field;

import com.alibaba.fastjson.JSONObject;
import com.dt.common.validation.annotation.Min;
import com.dt.common.validation.definition.MinDefinition;
import com.dt.common.validation.exception.BuildingException;
import com.dt.common.validation.impl.DefaultValidationDefinition;
import com.dt.common.validation.itf.PropertyGetter;

public class MinDefinitionParser extends DefinitionParser {

	@Override
	public DefaultValidationDefinition parseFromAnnotation(Field field, PropertyGetter propertyGetter) {
		Min min = field.getAnnotation(Min.class);
		if (min != null) {
			MinDefinition definition = new MinDefinition();
			definition.setMinValue(min.value());
			definition.setRawMsg(min.msg());

			return definition;
		}
		return null;
	}

	@Override
	public DefaultValidationDefinition parseFromJson(JSONObject metaValues) {
		Long value = metaValues.getLong("value");
		if (value == null) {
			throw new BuildingException("Min value must defined.");
		}

		MinDefinition definition = new MinDefinition();
		definition.setMinValue(value);
		definition.setRawMsg(this.getMsgDefinition(metaValues, true));

		return definition;
	}

}
